package com.socket;

import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.INonBlockingConnection;

import com.login.UserRegister;
import com.util.Constant;

public class ServerDataHandler implements SocketDataHandler {

	private SwingChatServer chatServer = null;

	public boolean onData(INonBlockingConnection connection)
			throws IOException, BufferUnderflowException,
			ClosedChannelException, MaxReadSizeExceededException {

		byte[] data = connection.readBytesByLength(connection.available());

		// get the client message
		String message = new String(data, 3, data.length - 3, "UTF-8");

		if (message.length() > Constant.CMD_LENGTH) {
			String cmd = message.substring(0, Constant.CMD_LENGTH);

			if (cmd.equals(Constant.CMD_SEND_TO_USER)) {
				sendMessageToUser(message);
			} else if (cmd.equals(Constant.CMD_SEND_TO_GROUP)) {
				sendMessageToGroup(message);
			} else if (cmd.equals(Constant.CMD_LOGIN)) {
				login(message, connection);
			} else if (cmd.equals(Constant.CMD_REGIST)) {
				regist(message, connection);
			} else if (cmd.equals(Constant.CMD_LOGOUT)) {
				logout(message, connection);
			} else if (cmd.equals(Constant.CMD_CREATE)) {
				create(message, connection);
			}
		}// end if
		return true;
	}

	private void regist(String msg, INonBlockingConnection connection) {
		String key = msg.substring(Constant.FROM_USER_ID_BEGIN,
				Constant.FROM_USER_ID_END);

		DASocketServer.userHash.put(key, connection);
	}// end register

	@Override
	public boolean onConnectException(INonBlockingConnection connection,
			IOException ioe) throws IOException {
		return true;
	}

	@Override
	public boolean onConnect(INonBlockingConnection connection)
			throws IOException, BufferUnderflowException,
			MaxReadSizeExceededException {
		System.out.println("in onConnect");
		return true;
	}

	@Override
	public boolean onDisconnect(INonBlockingConnection connection)
			throws IOException {
		return true;
	}

	public void setChatServer(SwingChatServer chat) {
		this.chatServer = chat;
	}

	public SwingChatServer getChatServer() {
		return this.chatServer;
	}

	private void sendMessageToUser(String message) {
	}

	private void sendMessageToGroup(String message) {
	}

	private void login(String message, INonBlockingConnection connection) {
	}

	private void logout(String message, INonBlockingConnection connection) {
	}

	private void create(String message, INonBlockingConnection connection) {
		String userName = message.substring(Constant.FROM_USER_ID_BEGIN,
				Constant.FROM_USER_ID_END);
		String userPass = message.substring(Constant.USER_PASS_BEGIN,
				Constant.USER_PASS_END);
		String result = new UserRegister(userName, userPass).regist();
		try {
			byte[] encodeMessage = String.valueOf(
					result + Constant.getServerID()).getBytes("UTF-8");
			connection.write(encodeMessage);
		} catch (BufferOverflowException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}